/**
 *
 *
 *
 */
import axios from 'axios'
import qs from 'qs'
import ca from "element-ui/src/locale/lang/ca";

const CancelToken = axios.CancelToken
const axiosMap = {},
  cancelTokenMap = {}
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
const instance = axios.create({
  transformRequest: [(data, headers) => {
    return qs.stringify(data)
  }],
  withCredentials: true,
  cancelToken: new CancelToken(cancel => {
  
  })
  
})
instance.interceptors.request.use(config => {
  const source = CancelToken.source()
  if (config.abort) {
    let oldSource = cancelTokenMap[config.url]
    if (oldSource) {
      oldSource.cancel()
    }
  }
  cancelTokenMap[config.url] = source
  return config
})
instance.interceptors.response.use((res) => {
  return res.data
})

function cancel(url) {
  let source = cancelTokenMap[url]
  source && source.cancel()
}

instance.cancelToken = cancel
instance.install = Vue => {
  Vue.prototype.$axios = instance
  Vue.prototype.$cancel = cancel
}
export default instance
